package service

import (
	"context"
	"fmt"
	"strconv"
	"time"
	"video-srv/basic/config"
	__ "video-srv/basic/proto"
	"video-srv/handler/model"
)

type Server struct {
	__.UnimplementedVideoServer
}

// VideoAdd 视频添加
func (s *Server) VideoAdd(_ context.Context, in *__.VideoAddReq) (*__.VideoAddResp, error) {
	//参数校验
	if in.Title == "" || in.Region == "" || in.Intro == "" || in.Year == "" || in.Image == "" || in.Cation == "" {
		return nil, fmt.Errorf("视频参数不能为空")
	}
	var video model.Video
	video = model.Video{
		Title:     in.Title,
		Intro:     in.Intro,
		Image:     in.Image,
		Cation:    in.Cation,
		Region:    in.Region,
		Year:      in.Year,
		ViewCount: 0,
		LikeCount: 0,
		Collect:   0,
		ComCount:  0,
		Host:      0,
	}
	config.DB.Create(&video)
	return &__.VideoAddResp{
		VideoId: int64(video.ID),
	}, nil
}

// VideoShow 视频详情
func (s *Server) VideoShow(_ context.Context, in *__.VideoShowReq) (*__.VideoShowResp, error) {

	var video model.Video

	config.DB.Debug().Where("id=?", in.VideoId).First(&video)

	//dx.SendSms(in.Phone, strconv.Itoa(code))
	//config.Rdb.Set(config.Ctx, CodeKey, code, time.Minute*60)

	return &__.VideoShowResp{
		VideoId: int64(video.ID),
		Title:   video.Title,
		Intro:   video.Intro,
		Image:   video.Image,
		Cation:  video.Cation,
		Region:  video.Region,
		Year:    video.Year,
	}, nil
}

// History 历史记录
func (s *Server) History(_ context.Context, in *__.HistoryReq) (*__.HistoryResp, error) {

	//参数校验
	if in.VideoId == 0 || in.UserId == 0 || in.EpisodeId == 0 || in.Progress == 0 || in.WatchTime == "" {
		return nil, fmt.Errorf("参数不能为空")
	}

	var history model.History

	history = model.History{
		UserId:    in.UserId,
		VideoId:   in.VideoId,
		EpisodeId: in.EpisodeId,
		Progress:  in.Progress,
		WatchTime: in.WatchTime,
	}

	config.DB.Create(&history)
	return &__.HistoryResp{
		HistoryId: int64(history.ID),
	}, nil
}

// Episode 剧集列表
func (s *Server) Episode(_ context.Context, in *__.EpisodeReq) (*__.EpisodeResp, error) {

	var episode model.Episode

	config.DB.Debug().Where("id=?", in.EpisodeId).First(&episode)

	return &__.EpisodeResp{
		VideoId:    episode.VideoId,
		Title:      episode.Title,
		EpisodeNum: episode.EpisodeNum,
		Duration:   episode.Duration,
	}, nil
}

// HistoryList 历史列表
func (s *Server) HistoryList(_ context.Context, in *__.HistoryListReq) (*__.HistoryListResp, error) {

	var hist model.History

	config.DB.Debug().Where("id=?", in.HistoryId).First(&hist)

	return &__.HistoryListResp{
		UserId:    hist.UserId,
		VideoId:   hist.VideoId,
		EpisodeId: hist.EpisodeId,
		Progress:  hist.Progress,
		WatchTime: hist.WatchTime,
	}, nil
}

// Host 视频热度值
func (s *Server) Host(_ context.Context, in *__.HostReq) (*__.HostResp, error) {
	//参数校验
	if in.Collect == 0 || in.ComCount == 0 || in.LikeCount == 0 || in.ViewCount == 0 {
		return nil, fmt.Errorf("参数不能为空")
	}

	var host model.Host

	host = model.Host{
		ViewCount: in.ViewCount,
		LikeCount: in.LikeCount,
		Collect:   in.Collect,
		ComCount:  in.ComCount,
		HostNum:   float64(in.HostNum),
	}

	//热度计算公式
	HostNum := float64(in.ViewCount)*0.6 + float64(in.LikeCount)*0.2 + float64(in.ComCount)*0.1 + float64(in.Collect)*0.1

	config.DB.Create(&host)

	return &__.HostResp{
		HostNum: float32(HostNum),
	}, nil
}

// HostList 热度统计
func (s *Server) HostList(_ context.Context, in *__.HostListReq) (*__.HostListResp, error) {

	var ho model.Host

	config.DB.Debug().Where("id=?", in.HostId).First(&ho)

	//redis
	hostKey := "host:" + strconv.FormatInt(in.HostId, 10)

	config.Rdb.Set(config.Ctx, hostKey, ho, time.Minute*30)

	return &__.HostListResp{
		ViewCount: ho.ViewCount,
		LikeCount: ho.LikeCount,
		Collect:   ho.Collect,
		ComCount:  ho.ComCount,
		HostNum:   float32(ho.HostNum),
	}, nil
}
